package com.fengli.task.controller;

import com.fengli.task.dao.WeekSummaryMapper;
import com.fengli.task.entity.WeekSummaryEntity;
import com.fengli.task.uitl.Result;
import com.fengli.task.uitl.enumType.Code;
import com.fengli.task.uitl.exception.ParamException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 该类提供与周总结相关的 接口
 * @Author: fengli
 * @Date 5/11/19 9:24 PM
 */
public class WeekSummaryController {
    @Autowired
    private WeekSummaryMapper weekSummaryDao;

    @ResponseBody
    @RequestMapping(value = "updateWeekSummary",method = RequestMethod.POST)
    public Result updateWeekSummary(@Validated @RequestBody WeekSummaryEntity weekSummaryEntity, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            List<ObjectError> list = bindingResult.getAllErrors();
            throw new ParamException(list.get(0).getDefaultMessage());
        }
        weekSummaryDao.updateWeekSummary(weekSummaryEntity);
        Result result = new Result("添加周总结成功", Code.SUCCESS,weekSummaryEntity.getId());
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "addWeekSummary", method = RequestMethod.POST)
    public Result addWeekSummary(@Validated @RequestBody WeekSummaryEntity weekSummaryEntity, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            List<ObjectError> list = bindingResult.getAllErrors();
            throw new ParamException(list.get(0).getDefaultMessage());
        }
        weekSummaryDao.addWeekSummary(weekSummaryEntity);
        Result result = new Result("添加周总结成功", Code.SUCCESS,weekSummaryEntity.getId());
        return result;
    }
}
